package com.ja.openchat.views;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.tencent.smtt.sdk.TbsReaderView;
import com.ycbjie.webviewlib.utils.X5LogUtils;

public class AppFileReaderView extends FrameLayout implements TbsReaderView.ReaderCallback {

    private TbsReaderView mTbsReaderView;
    private Context context;

    public AppFileReaderView(Context context) {
        this(context, null, 0);
    }

    public AppFileReaderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AppFileReaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTbsReaderView = getTbsReaderView(context);
        this.addView(mTbsReaderView, new LinearLayout.LayoutParams(-1, -1));
        this.context = context;
    }


    private TbsReaderView getTbsReaderView(Context context) {
        return new TbsReaderView(context, this);
    }

    /**
     * 初始化完布局调用此方法浏览文件
     * @param filePath  文件路径
     */
    public void show(String filePath) {
        if (!TextUtils.isEmpty(filePath)) {
            try {
                //加载文件
                Bundle localBundle = new Bundle();
                localBundle.putString("filePath", filePath);
                localBundle.putString("tempPath", Environment.getExternalStorageDirectory() + "/" + "TbsReaderTemp");
                if (this.mTbsReaderView == null) {
                    this.mTbsReaderView = getTbsReaderView(context);
                }
                boolean bool = this.mTbsReaderView.preOpen(getFileType(filePath), false);
                if (bool) {
                    this.mTbsReaderView.openFile(localBundle);
                }else{
                    if(callback!=null){
                        callback.onError();
                    }
                }
            } catch (Exception e){
                e.printStackTrace();
                if(callback!=null){
                    callback.onError();
                }
            }
        } else {
            X5LogUtils.d("TAG"+ "文件路径无效！");
            if(callback!=null){
                callback.onError();
            }
        }
    }

    @Override
    public void onCallBackAction(Integer integer, Object o, Object o1) {
        X5LogUtils.d("TAG"+ integer+""+o+o1);
//        if(integer==12&&callback!=null){
//            callback.onSuccess();
//        }
    }

    public interface OnTBSOpenCallback{
        void onSuccess();
        void onError();
    }

    private OnTBSOpenCallback callback;

    public void setCallback(OnTBSOpenCallback callback){
        this.callback = callback;
    }

    /**
     * 务必在onDestroy方法中调用此方法，否则第二次打开无法浏览
     */
    public void stop() {
        if (mTbsReaderView != null) {
            mTbsReaderView.onStop();
        }
    }

    /***
     * 获取文件类型
     */
    public static String getFileType(String paramString) {
        String str = "";
        if (TextUtils.isEmpty(paramString)) {
            return str;
        }
        int j = paramString.lastIndexOf("/");
        int i;
        int realI;
        if(j>0){
            i = paramString.substring(j+1).lastIndexOf('.');
            realI = j+1+i;
        }else{
            i = paramString.lastIndexOf('.');
            realI = i;
        }
        if (i <= -1) {
            return str;
        }
        return paramString.substring(realI + 1);
    }

}

